// pages/suggest/suggest.js
const app = getApp();
Page({
 
  data: {
    //留言
    liuyan:'',
    texts: "至少5个字",
    min: 5, //最少字数
    max: 200, //最多字数 (根据自己需求改变)
    // 图片
    img_arr: [],
    //联系方式
    tel:'',

  },
  // 建议
  getSuggest: function (e) {
    // 获取输入框的内容
    // console.log(e.detail)
    this.setData({
      liuyan: e.detail.value
    })
    // 获取输入框内容的长度
    var len = parseInt(e.detail.value.length);
    //  console.log(liuyan)
    if (len <= this.data.min)
      this.setData({
        texts: "最低五个字"
      })
    else if (len > this.data.min)
      this.setData({
        texts: " "
      })
    if (len > this.data.max) return;
    this.setData({
      currentWordNumber: len//当前字数  
    });
  },
  //上传图片
  upimg: function (e) {
    var _this = this;
    var imgIndex = e.currentTarget.dataset.index
    wx.chooseImage({
      count: 6,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var arrImg = [];
        var tempFilePathsL = res.tempFilePaths.length;
        for (var i = 0; i < tempFilePathsL; i++) {
          wx.uploadFile({
            url:'https://xcx.yhzip.com/api.php/fabu/uploadify',
            filePath: res.tempFilePaths[i],
            name: 'file',
            success(res) {
              console.log(res.data);
              var data = JSON.parse(res.data);
              console.log(data);
              data = data.imgurl;
              console.log(data);
              arrImg.push(data);
              console.log(arrImg)
              if (i == tempFilePathsL) {
                _this.setData({
                  img_arr: arrImg
                })
              }
            }
          })
        }
      }
    })
  },
  //删除图片
  delimg: function (e) {
    let _this = this;
    var imgIndex = e.currentTarget.dataset.index;
    console.log(imgIndex)
    _this.data.img_arr.splice(imgIndex, 1);
    _this.setData({
      img_arr: _this.data.img_arr
    })
  },
  //点击放大图片
  previewImg: function (e) {
    console.log(e.currentTarget.dataset.index);
    var index = e.currentTarget.dataset.index;
    var img_arr = this.data.img_arr;
    wx.previewImage({
      current: img_arr[index],     //当前图片地址
      urls: img_arr,               //所有要预览的图片的地址集合 数组形式

    })
  },
  //联系方式
  bindKeytel: function (e) {
    let _this = this
    _this.setData({
      tel: e.detail.value
    })
  },
  // 提交
  formSubmit: function () {
    let _this = this

    var phonetel = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9]|19[0-9])\d{8}$$/;

    if (_this.data.tel == '') {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return false
    } else if (!phonetel.test(_this.data.tel)) {
      wx.showToast({
        title: '手机号有误，请重新填写',
        icon: 'none',
        duration: 1500
      })
      return false;
    }

    wx.request({
      url:'https://xcx.yhzip.com/api.php/my/mgadd',
      method: 'POST',
      data: {
        uid: app.globalData.user.id,
        desc:_this.data.liuyan,
        imgurl: _this.data.img_arr,
        tel: _this.data.tel,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        console.log(res.data)
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000,
          success: function () {
            setTimeout(function () {
              wx.switchTab({
                url: '/pages/myself/myself'
              })
            }, 2000)
          }
        })

      }
    })
  },

  onLoad: function (options) {

  },

})